package com.woniuxy.usercoachassemble.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.usercoachassemble.entity.User;
import com.woniuxy.usercoachassemble.dao.UserMapper;
import com.woniuxy.usercoachassemble.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-07-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @description:TODO(根据根据邮箱查询user表)
     * @param email 邮箱
     * @return:  com.woniuxy.usercoachassemble.entity.User
     * @author: Fy浪迹、
     * @date: 2020/7/9 16:49
     */
    @Override
    public User selectUserByEmail(String email){
        User user = query().eq("email", email).one();
        return user;
    }


    /**
     * @description:TODO(根据邮箱注册用户)
     * @param user 1.email 2.password 3 role==2
     * @return:  com.woniuxy.usercoachassemble.entity.User 新增的未激活用户
     * @author: Fy浪迹、
     * @date: 2020/7/9 17:19
     */
    @Override
    public User registerByEmail(User user) {
        user.setA(1);
        boolean b = save(user);

        // 为新增成功
        if(!b){
            return null;
        }
        // 返回新增的用户
        User user1 = selectUserByEmail(user.getEmail());


        return user1;
    }

    /**
     * @description:TODO(根据用户id查询用户)
     * @param userId
     * @return:  com.woniuxy.usercoachassemble.entity.User
     * @author: Fy浪迹、
     * @date: 2020/7/9 19:23
     */
    @Override
    public User selectUserById(Integer userId) {

        return query().eq("user_id",userId).one();
    }

    /**
     * @description:TODO(根据用户id修改账号状态)
     * @param userId
     * @param status
     * @return:  int
     * @author: Fy浪迹、
     * @date: 2020/7/10 11:19
     */
    @Override
    public int updateUserStatusById(Integer userId, int status) {
        User user = new User();
        user.setUserId(userId);
        user.setStatus(status);
//        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.set("status",status);
//        updateWrapper.eq("user_id",userId);
//        boolean b = update(updateWrapper);

        boolean b = update().set("status", status).eq("user_id", userId).update();
        if(!b){
            return -1;
        }
        return 1;
    }


    /**
     * @description:TODO(根据电话号码获取user)
     * @param tel
     * @return:  com.woniuxy.usercoachassemble.entity.User
     * @author: Fy浪迹、
     * @date: 2020/7/10 11:19
     */
    @Override
    public User selectUserByTel(String tel) {
        return query().eq("tel",tel).one();
    }

    /**
     * @description:TODO(根据电话,角色注册用户)
     * @param tel
     * @param role
     * @return:  com.woniuxy.usercoachassemble.entity.User
     * @author: Fy浪迹、
     * @date: 2020/7/10 17:31
     */
    @Override
    public User registerByTel(String tel, Integer role) {
        User user = new User();
        user.setA(1);
        user.setTel(tel);
        user.setRoleId(role);
        user.setStatus(2);
        boolean b = save(user);
        if(!b){
            return null;
        }
        User user1 = selectUserByTel(tel);

        return user1;
    }

    /**
     * @description:TODO(根据电话、角色注册)
     * @param tel
     * @param role
     * @return:  com.woniuxy.usercoachassemble.entity.User
     * @author: Fy浪迹、
     * @date: 2020/7/10 17:32
     */
    @Override
    public User registerByTelAndPassword(String tel, Integer role, String password) {
        User user = new User();
        user.setA(1);
        user.setTel(tel);
        user.setRoleId(role);
        user.setStatus(2);
        user.setPassword(password);
        boolean b = save(user);
        if(!b){
            return null;
        }
        User user1 = selectUserByTel(tel);

        return user1;
    }

    /**
     * @description:TODO(根据电话号码修改密码)
     * @param tel
     * @param password
     * @return:  com.woniuxy.usercoachassemble.entity.User
     * @author: Fy浪迹、
     * @date: 2020/7/10 17:30
     */
    @Override
    public User updatePasswordByTel(String tel, String password) {

        User user = new User();
        boolean b = update().set("password", password).eq("tel", tel).update(user);
        if(!b){
            return  null;
        }
        System.out.println(user);
        return user;
    }

    @Override
    public int updatePasswordByEmail(String email, String password) {
        boolean b = update().set("password", password).eq("email", email).update();
        if(!b){
            return -1;
        }
        return 1;
    }

    @Override
    public User selectUserByUsername(String username) {
        return query().eq("username",username).one();
    }

    @Override
    public Map<String, Object> selectUserMoreById(int userId) {
       Map<String, Object> user = userMapper.selectUserMoreById(userId);
        return user;
    }
}
